import type { IBase } from '@/types/request';
import type { Transform, TransformBase } from '@/types/table-transform';

// 基础数据的转换
export function baseTransform(base: IBase): TransformBase {
  function handleGmt(gmt: string) {
    return {
      text: new Date(gmt).toLocaleString(),
      value: gmt,
    };
  }

  return {
    id: {
      text: String(base.id),
      value: base.id,
    },
    del: {
      value: !!base.del,
      text: base.del ? '已删除' : '未删除',
    },
    gmt_create: handleGmt(base.gmt_create),
    gmt_modified: handleGmt(base.gmt_modified),
  };
}

type ToTransform = IBase & Record<string, any>;

// 将接口数据保留原始值，加入修改后的值，原始值方便传入接口，修改的值方便展示
export function commonTransform<O extends ToTransform>(o: O): Transform<O> {
  return {
    ...Object.entries(o).reduce((acc, [key, value]) => {
      return {
        ...acc,
        [key]: {
          value,
          text: value,
        },
      };
    }, {} as Transform<O>),
    ...baseTransform(o as IBase),
  };
}

